<?php
namespace App\Http\Controllers;

use Request;
use DB;
use App\Models\Msg\msgIcnBelikedM;
use App\Models\Msg\msgBestoredM;
use App\Common\RouteColl;
use App\Http\Controllers\Controller;
use App\Models\Msg\msgCommentM;
use Session;
use App\Models\Msg\msgAddFollow;
use App\Models\Msg\msgCmtBelikedM;
use App\Models\Msg\msgRpM;

/**
 * 消息 过滤类
 * 过滤对应 的url
 * @author lee
 *
 */
class  MsgFilterController extends Controller
{
    private $_action=null;
    private $_params=null;
    
    public function  __construct(){
        $urlParts=Request::segments();
        if(count($urlParts) == 0){return;}
        $this->_action=$urlParts[0];
        $this->_params=array_slice($urlParts, 1);
        $this->msgDistribute();
    }
    
    public function __call($name,$arguments){
        echo "Calling object method '$name' is not existed \n";
    }
    
    public  function __get($name){
        return $this->$name;
    }
   
    /***
     * 消息匹配 和 写入
     */
    public  function msgDistribute(){
        $rcdMsgRoutesObj=new RouteColl();
        //excute message record function
        if($rcdMsgRoutesObj->isMsgUrl( $this->_action) &&
                method_exists($this,$this->_action)){
            // 执行消息 写入
           call_user_func([$this,$this->_action]);
        }
    }
    

    /*********** 消息 写入*******************/
    public  function testP(){
//        $this->addFollow();
//        $this->removeFollow();

//        $this->addRp();
    }
    
    
    public function test(){
        
        
//         $this->iconPlusFavour();
//         $this->iconPlusStore();
//         $this->cmtPlusFavour();

        
//         $this->dltRp();
//         $this->deltCmt();

//         $this->iconMinFavour();
//         $this->iconMinStore();

        
//         $this->cmtMinFavour();
        

    }
    
    /**
     * 添加粉丝 消息
     */
    public function addFollow(){
        DB::beginTransaction();
        try {
            
            $inc= json_decode(Request::input('userInfo'));
            $userid=$inc->userid; // userid
            $followid=$inc->followid; // follow id

            // add item
            msgAddFollow::addItem(['userid'=>$userid,'followedid'=>$followid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
    
    
    /**
     * 移除 粉丝 消息
     */
    public function removeFollow(){
        DB::beginTransaction();
        try {
            $inc= json_decode(Request::input('userInfo'));
            $userid=$inc->userid; // userid
            $followid=$inc->followid; // follow id
        
            // add item
            msgAddFollow::dltItem(['userid'=>$userid,'followedid'=>$followid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
    
    
     
    /**
     * 评论 点赞消息
     */
    public  function cmtPlusFavour(){
        DB::beginTransaction();
        try {
        
            $userid=$this->_params[1]; // userid
            $cmtid=$this->_params[0]; // cmtid
            $belikeduserid=$this->_params[2]  ;// beliked userid
        
            // add item
            msgCmtBelikedM::addItem(['userid'=>$userid,"cmtid"=>$cmtid,
                'belikeduserid'=>$belikeduserid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
    
    /**
     * 评论 取消点赞消息
     */
    public function cmtMinFavour(){
        DB::beginTransaction();
        try {
        
            $userid=$this->_params[1]; // userid
            $cmtid=$this->_params[0]; // cmtid        
            // add item
            msgCmtBelikedM::dltItem(['userid'=>$userid,"cmtid"=>$cmtid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
    
    
    /**
     * icon 点赞消息
     */
    public  function iconPlusFavour(){
        DB::beginTransaction();
        try {
            $userid=$this->_params[1]; // userid
            $iconid=$this->_params[0]; // iconid
            $belikeduserid=$this->_params[2]  ;// beliked userid
            // add item
            msgIcnBelikedM::addItem(['userid'=>$userid,'iconid'=>$iconid,
                'belikeduserid'=>$belikeduserid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
    
    /**
     * 取消 icon点赞
     */
    public  function iconMinFavour(){
        DB::beginTransaction();
        try {
            $userid=$this->_params[1]; // userid
            $iconid=$this->_params[0]; // iconid
            // add item
            msgIcnBelikedM::dltItem(['userid'=>$userid,'iconid'=>$iconid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
    
    /**
     * icon 被收藏 消息
     */
    public function iconPlusStore(){
        DB::beginTransaction();
        try {
            $userid=$this->_params[1]; // userid
            $iconid=$this->_params[0]; // iconid
            $belikeduserid=$this->_params[2]; // belikeduserid
            // add item
            msgBestoredM::addItem(['userid'=>$userid,'iconid'=>$iconid,
                'bestoreduserid'=>$belikeduserid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
    
    /**
     * 取消 icon 收藏 
     */
    public function iconMinStore(){
        DB::beginTransaction();
        try {
            $userid=$this->_params[1]; // userid
            $iconid=$this->_params[0]; // iconid
            // add item
            msgBestoredM::dltItem(['userid'=>$userid,'iconid'=>$iconid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
        
    }
    
    /***
     * icon 添加评论
     */
    public function addCmt(){
        
        DB::beginTransaction();
        try {
          
           if(Session::has('newCmtID')){
                $commentid=Session::get("newCmtID");
            }else{
                return;
            }      
            $inc= json_decode(Request::input('cmtInfo'));
            $userid=$inc->userid; // userid
            $iconid=$inc->iconfid; // iconid
            $becmtuserid=$inc->becmtuserid; // be commented userid// commentid
            // add item
            msgCommentM::addItem(['userid'=>$userid,
                'iconid'=>$iconid,'commentid'=>$commentid,'becmtuserid'=>$becmtuserid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
   

    /**
     * 删除 评论
     */
    public  function deltCmt(){
        DB::beginTransaction();
        try {
            $cmtid=$this->_params[0]; // commentid
            // add item
            msgCommentM::dltItem(['commentid'=>$cmtid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
    
    
    
    /**
     * 添加 回复 消息
     */
    public  function addRp(){
        DB::beginTransaction();
        try {
    
            if(Session::has('newRpID')){
                $rpid=Session::get("newRpID");
            }else{
                return;
            }
            $inc= json_decode(Request::input('rpInfo'));
            $userid=$inc->userid; // userid
            $cmtid=$inc->commentid;
            $berpuserid=$inc->rp_userid; // be reply userid
            // add item
            msgRpM::addItem(['userid'=>$userid,
                'berpuserid'=>$berpuserid,'cmtid'=>$cmtid,'rpid'=>$rpid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
    
    /**
     * 删除 回复 消息
     */
    public function dltRp(){
        DB::beginTransaction();
        try {
//             $userid=$this->_params[1]; // userid
            $rpid=$this->_params[0]; // reply id
            // add item
            msgRpM::dltItem(['rpid'=>$rpid]);
            DB::commit();
        }catch (QueryException $e){
            DB::rollBack();
        }
    }
    
    
    
    
    
}

?>